﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DataLayer.Types;
using ViewModel.Finance;

namespace MyWpfApp.Finance
{
    /// <summary>
    /// Interaction logic for CategoryListView.xaml
    /// </summary>
    public partial class CategoryListView : UserControl
    {
        private CategoryListViewModel viewModel;

        public CategoryListView()
        {
            InitializeComponent();
            viewModel = DataContext as CategoryListViewModel;
        }

        private void listView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            CategoryItem item = listView.SelectedItem as CategoryItem;
            ShowItemDialog(item);
        }

        private void ItemPropertiesClicked(object sender, RoutedEventArgs e)
        {
            CategoryItem item = listView.SelectedItem as CategoryItem;
            ShowItemDialog(item);
        }

        private void NewItemClicked(object sender, RoutedEventArgs e)
        {
            CategoryItem categoryItem = new CategoryItem();
            ShowItemDialog(categoryItem);

        }

        private void DeleteItem_Click(object sender, RoutedEventArgs e)
        {
            DeleteItem(listView.SelectedItem as CategoryItem);
        }

        private void ShowItemDialog(CategoryItem item)
        {
            CategoryPropertiesView propertiesView = new CategoryPropertiesView();
            propertiesView.DataContext = new CategoryPropertiesViewModel(item);
            propertiesView.ShowDialog();
        }


        private void DeleteItem(CategoryItem item)
        {
            if (MessageBox.Show("Are you sure you want to delete this item?\n\r\n\r" + item.Name) != MessageBoxResult.OK) return;

            viewModel.DeleteItem(item);
        }
    }
}
